var g_gameDataManager = require("g_gameDataManager");
var MessageManager = require("MessageManager");
cc.Class({
    extends: cc.Component,

    properties: {
        nameInputField:cc.EditBox,
        pwdInputField:cc.EditBox,
        douPwdInputField:cc.EditBox,
        registBtn:cc.Button
    },

    // use this for initialization
    onLoad: function () {

    },

    registerCallBack: function () {
        var nickStr = this.nameInputField.string.trim();
        var pwdStr = this.pwdInputField.string.trim();
        var douPwdStr = this.douPwdInputField.string.trim();
        if(pwdStr != douPwdStr)
        {
            console.log("pwd must equal. input again!");
            return;
        }
        g_gameDataManager.setUserName(nickStr);
        g_gameDataManager.setUserPassword(pwdStr);
        var url = "http://127.0.0.1:8080/register/?usr="+nickStr+"&pwd="+pwdStr;
        MessageManager.register(nickStr,pwdStr,function(msg){
            if(msg == "httperror")
            {
                console.log("httperror");
            }
            else
            {
                console.log(msg);
                var obj = JSON.parse(msg);
                var usrID = parseInt(obj.data.usrID);
                g_gameDataManager.setUserID(usrID);
                cc.director.loadScene('LoginScene');

            }
        }.bind(this));
    },


    httpRegister:function(url,timeout,callback)
    {
        console.log("httpRegister");
        var xhr = new XMLHttpRequest();
        var timedout = false;
        var timer = setTimeout(function(){
            timedout = true;
            xhr.abort();
            callback("httperror");
        },timeout);
        xhr.onreadystatechange = function(){
            if(timedout){ return;}
            clearTimeout(timer);
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400))
            {
                callback(xhr.responseText);
            }
        };
        xhr.open("GET",url,true);
        xhr.send();
    }


    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
